/*
 *  Copyright (C) 1993  Robert Davis
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of Version 2, or any later version, of 
 *  the GNU General Public License as published by the Free Software 
 *  Foundation.
 */


static char RCSId[]="$Id: DataOptionsPanel.m,v 1.10 1993/05/24 03:59:41 davis Exp $";


#import <appkit/Application.h>
#import <appkit/Cell.h>
#import <appkit/Panel.h>
#import <appkit/TextField.h>

#import <objc/NXStringTable.h>

#import "DataOptionsPanel.h"
#import "DataFilePane.h"
#import "DataFileThreeDPane.h"
#import "DataPane.h"
#import "EditMatrix.h"
#import "FunctionPane.h"
#import "FunctionObject.h"
#import "GnuplotPlot.h"
#import "NoDataPane.h"
#import "Pane.h"
#import "Status.h"
#import "SubObjectCategory.h"

#define	FUNCTION_PANE		0
#define	DATAFILE_PANE		1
#define	DATAFILE_THREED_PANE	2
#define	NO_PANE			3



@interface DataOptionsPanel (Private)

- _setupPane:(Pane *)aPane;
- _selectPane:(int)aPane;
- _swapPane:(Pane *)new;
- _updatePanel;			 /** Overridden from OptionsPanel (Private) **/

@end



@implementation DataOptionsPanel

- init
{
    NXSize interCellSpacing = {1.0, 1.0};

    [super init];

    [NXApp loadNibSection: "DataOptionsPanel.nib"
		    owner: self
		withNames: NO
		 fromZone: [self zone]];

    [panel setFrameUsingName:"DataOptionsPanel"];
    [panel setFrameAutosaveName:"DataOptionsPanel"];
    [panel setBecomeKeyOnlyIfNeeded:YES];

    isOwnerCurrent = YES;
    isMult = NO;

    return self;
}


- free
{
    [stringSet free];
    [functionPane free];
    [dataFilePane free];
    [dataFileThreeDPane free];
    [noPane free];

    return [super free];
}


- setOwner:anObject
{
    if ([anObject respondsTo:@selector(functionsMatrix)]) {
	owner = anObject;
	functionsMatrix = [owner functionsMatrix];
    }

    return self;
}


- forceUpdate
{
    id		mainWindow;

    if (isOwnerCurrent && (mainWindow = [NXApp mainWindow])) {

	status = [[mainWindow delegate] status];
	function = [[functionsMatrix selectedCell] subObject];
	isMult = !function && [functionsMatrix multipleCellsSelected];

    } else {

	status = nil;
	function = nil;
	isMult = NO;
    }

    [self _updatePanel];

    return self;
}


- windowDidUpdate:sender
{
    id		mainWindow;
    id		newStatus, newFunction;
    BOOL	newMult;

    if (isOwnerCurrent && (mainWindow = [NXApp mainWindow])) {

	newStatus = [[mainWindow delegate] status];
	newFunction = [[functionsMatrix selectedCell] subObject];
	newMult = [functionsMatrix multipleCellsSelected];

    } else {

	newStatus = nil;
	newFunction = nil;
	newMult = NO;

    }

    if ((newStatus != status) || (newFunction != function)
	|| (newMult != isMult)) {
	status = newStatus;
	function = newFunction;
	isMult = newMult;
	[self _updatePanel];
    }

    return self;
}




- ownerDidSwapIn:sender
{
    isOwnerCurrent = YES;
    [self forceUpdate];

    return self;
}


- ownerDidSwapOut:sender
{
    isOwnerCurrent = NO;
    [self forceUpdate];

    return self;
}


- function
{
    return function;
}


- functionsMatrix
{
    return functionsMatrix;
}


- setFunctionTitle:sender
{
    int		i;
    Cell	*cell;
    id		lastHighlightedObject = nil;
    BOOL	didChange = NO;

  
    /* Set title of all selected functions. */

    for (i = [functionsMatrix cellCount] - 1 ; i >= 0 ; i--) {
	cell = [functionsMatrix cellAt:i:0];
	if ([cell isHighlighted])  {
	    lastHighlightedObject = [cell subObject];
	    [lastHighlightedObject setTitle: [titleField stringValue]];
	    didChange = YES;
	}
    }

    /*  The subObjects may have fixed the title  */
    [titleField setStringValue:[lastHighlightedObject title]];
  
    /* 
     *  We changed the functions directly instead of going through 
     *  status, so we must inform status of the change.
     */

    if (didChange)
	[status reportSettingsChange:self];

    return self;
}


// Shuts up the compiler about unused RCSId
- (const char *) rcsid
{
    return RCSId;
}



@end





@implementation DataOptionsPanel (Private)

- _setupPane:(Pane *)aPane
{
    if (aPane) {
        View	*panesView = [aPane view];

        [[panel contentView] addSubview: panesView];
        [[panesView allocateGState] lockFocus];
        [panesView unlockFocus];
        [panesView moveTo:0:0];
    }

    return self;
}
        

- _selectPane:(int)aPane
{
    switch (aPane) {

    case FUNCTION_PANE:
        if (!functionPane) {
            functionPane = [[FunctionPane allocFromZone:[self zone]] init];
            [self _setupPane:functionPane];
        }
        [self _swapPane:functionPane];
        break;

    case DATAFILE_PANE:
        if (!dataFilePane) {
            dataFilePane = [[DataFilePane allocFromZone:[self zone]] init];
            [self _setupPane:dataFilePane];
        }
        [self _swapPane:dataFilePane];
        break;

    case DATAFILE_THREED_PANE:
        if (!dataFileThreeDPane) {
            dataFileThreeDPane =
		[[DataFileThreeDPane allocFromZone:[self zone]] init];
            [self _setupPane:dataFileThreeDPane];
        }
        [self _swapPane:dataFileThreeDPane];
        break;

    default:
        if (!noPane) {
            noPane = [[NoDataPane allocFromZone:[self zone]] init];
            [self _setupPane:noPane];
        }
        [self _swapPane:noPane];
        break;

    }

    return self;
}



- _swapPane:(Pane *)new
{
    /*  
     *  If the new pane is not already visible, move it into the panel.
     */
    
    if (new != currentPane) {

        [[panel contentView] replaceSubview:[currentPane view]
				       with:[new view]];
        [currentPane didSwapOut:self];

	/*  
	 *  Notice we give ourselves as the "doc" when updating one of 
	 *  our Panes.  It may then query us for current function.
	 */
        [[new didSwapIn:self] updateStatus:status doc:self];
//        [panel display];

        [panel setTitle:[new title]];
        [panel setMiniwindowIcon:[new icon]];

        currentPane = new;

    }

    return self;
}



- _updatePanel
{
    BOOL	enabled = (isOwnerCurrent && status);// is status necessary?

    [panel disableDisplay];

    if (enabled && isMult) {

	[stringField setStringValue:[stringSet valueForStringKey:"multiple"]];
	[titleField setStringValue:""];
	[self _selectPane:FUNCTION_PANE];

    } else if (enabled && function) {

	[stringField setStringValue:[function stringValue]];
	[titleField setStringValue:[function title]];

	if ([function isDataFile]) {
	    if ([status isThreeD])
		[self _selectPane:DATAFILE_THREED_PANE];
	    else
		[self _selectPane:DATAFILE_PANE];
	} else
	    [self _selectPane:FUNCTION_PANE];

    } else {

	[stringField setStringValue:[stringSet valueForStringKey:"none"]];
	[titleField setStringValue:""];
	[self _selectPane:NO_PANE];

    }

    [titleField setEnabled:enabled && (isMult || function)];
    [currentPane updateStatus:status doc:self];

    [panel reenableDisplay];
    [panel display];

    return self;
}

@end
